home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_08 / 9n08018a < prev    next >
Text File  |  1991-07-08  |  2KB  |  50 lines

  1. /* fmod function */
  2. #include "xmath.h"
  3.  
  4. double (fmod)(double x, double y)
  5.         {       /* compute fmod(x, y) */
  6.         const short errx = _Dtest(&x);
  7.         const short erry = _Dtest(&y);
  8.  
  9.         if (errx == NAN || erry == NAN || errx == INF || erry == 0)
  10.                 {       /* fmod undefined */
  11.                 errno = EDOM;
  12.                 return (errx == NAN ? x : erry == NAN ? y : _Nan._D);
  13.                 }
  14.         else if (errx == 0 || erry == INF)
  15.                 return (x);     /* fmod(0,nonzero) or fmod(finite,INF) 
  16. */
  17.         else
  18.                 {       /* fmod(finite,finite) */
  19.                 double t;
  20.                 short n, neg, ychar;
  21.         
  22.                 if (y < 0.0)
  23.                         y = -y;
  24.                 if (x < 0.0)
  25.                         x = -x, neg = 1;
  26.                 else
  27.                         neg = 0;
  28.                 for (t = y, _Dunscale(&ychar, &t), n = 0; ; )
  29.                         {       /* subtract |y| until |x|<|>y| */
  30.                         short xchar;
  31.         
  32.                         t = x;
  33.                         if (n < 0 || _Dunscale(&xchar, &t) == 0
  34.                                 || (n = xchar - ychar)  0)
  35.                                 return (neg ? -x : x);
  36.                         for (; 0 <= n; --n)
  37.                                 {       /* try to subtract |y|*2^n 
  38. */
  39.                                 t = y, _Dscale(&t, n);
  40.                                 if (t <= x)
  41.                                         {
  42.                                         x -= t;
  43.                                         break;
  44.                                         }
  45.                                 }
  46.                         }
  47.                 }
  48.         }
  49.  
  50.